package cc.plumgo.ss.ui.page;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

import cc.plumgo.j2me.utils.CustomPage;
import cc.plumgo.j2me.utils.StringUtils;
import cc.plumgo.ss.core.Console;
import cc.plumgo.ss.ui.UIEngine;
import cc.plumgo.ss.ui.text.MsgManager;

public class TextPage extends CustomPage {
	public static final int TEXT_HIDE = 0;
	public static final int TEXT_SHOW = 1;
	public static final int TEXT_STATE_NUM = 3;

	private Font font;
	private int txtType;
	public TextPage(int type) {
		super();
		txtType = type;
	}
	
	public void process(int keyStates, Graphics g) {
		switch (state) {
		case TEXT_HIDE: {
			switch(txtType) {
			case MsgManager.MSG_BACK : {
				Console.getInstance().getGameListener().restart();
				break;
			}
			case MsgManager.MSG_COMP : {
				Console.getInstance().getGameListener().showPage(UIEngine.UI_MENU);
			}
			}
			return;
		}
		case TEXT_SHOW: {
			if ((keyStates & GameCanvas.FIRE_PRESSED) != 0) {
				state = TEXT_HIDE;
			}
			loadFont();
			g.setFont(font);
			int back = g.getColor();
			g.setColor(0xFFFFFFFF);
			String text = MsgManager.getText(txtType);
			StringUtils.adjustDrawString(g, text, 200, 20, 20, 20);
			g.setColor(back);
			break;
		}
		}
		
	}
	
	public void loadFont() {
		if (font == null) {
			try {
				font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
			} catch(Exception e) {
				font = Font.getDefaultFont();
			}
		}
	}
}
